package com.yuwenxin.dao.impl;

import com.yuwenxin.bean.Album;
import com.yuwenxin.bean.Music;
import com.yuwenxin.bean.MusicList;
import com.yuwenxin.bean.User;
import com.yuwenxin.core.BaseDaoImpl;
import com.yuwenxin.core.BaseQueryModel;
import com.yuwenxin.dao.*;

import java.sql.ResultSet;
import java.sql.Date;

public class MusicDaoImpl extends BaseDaoImpl implements MusicDao {
    @Override
    public String getSelectAllSql() {
        return "select * from music";
    }

    @Override
    public String getSelectByNameSql() {
        return "select * from music where musicName = ?";
    }

    @Override
    public String getFuzzyFindSql() {
        return "select * from music where musicName like ?";
    }

    @Override
    public String getUpdateSql(Object object) {
        return null;
    }

    @Override
    public String getDeleteSql() {
        return "delete from music where idmusic = ?";
    }

    @Override
    public String getSelectByIdSql() {
        return "select * from music where idmusic = ?";
    }

    @Override
    public String getInsertSql(Object object) {
        Music _music = (Music) object;

        String musicName = _music.getMusicName();
        String lyrics = _music.getLyrics();
        String filePath = _music.getFilePath();
        String musicPhotoPath;
        if (_music.getMusicPhotoPath() == null){
            musicPhotoPath = MusicDao.DEFAULT_PATH + "defalt_photo.jpg";
        }else {
            musicPhotoPath = _music.getMusicPhotoPath();
        }

        Date postDate = new Date(new java.util.Date().getTime());

//        int belongedAlbumid = _music.getBelongedAlbumId();
//        int belongedCombinationid = _music.getBelongedCombinationId();
//        int belongedMusiclistid = _music.getBelongedMusiclistId();
//        int belongedMusicianid = _music.getBelongedMusicianId();
        int belongedPosterid = _music.getPosterId();
        return "insert into music (musicName,lyrics,filePath,musicPhotoPath,postDate,belongedPosterid) values ('"
                + musicName + "','"
                + lyrics + "','"
                + filePath + "','"
                + musicPhotoPath + "','"
                + postDate + "',"
                + belongedPosterid + ")";
//        return "insert into music (musicName,lyrics,filePath,musicPhotoPath,postDate,belongedAlbumid,belongedCombinationid," +
//                "belongedMusicianid,belongedMusiclistid,belongedPosterid) values ('"
//                + musicName + "','"
//                + lyrics + "','"
//                + filePath + "','"
//                + musicPhotoPath + "','"
//                + postDate + "',"
//                + belongedAlbumid + ","
//                + belongedCombinationid + ","
//                + belongedMusicianid+ ","
//                + belongedMusiclistid + ","
//                + belongedPosterid + ")";
    }

    @Override
    public String getConditionSql(BaseQueryModel queryModel) {
        return null;
    }

    @Override
    public Object toEntity(ResultSet rs) throws Exception {
        Music music = new Music();

        AlbumDao albumDao = new AlbumDaoImpl();
        UserDao userDao = new UserDaoImpl();
        MusicListDao musicListDao = new MusicListDaoImpl();
        CommentDao commentDao = new CommentDaoImpl();
        CombinationDao combinationDao = new CombinationDaoImpl();

        music.setId(rs.getInt("idmusic"));
        music.setPlayNum(rs.getInt("playNum"));
        music.setLikeNum(rs.getInt("likeNum"));
        music.setCollectNum(rs.getInt("collectNum"));
        music.setMusicName(rs.getString("musicName"));
        music.setLyrics(rs.getString("lyrics"));
        music.setMusicPhotoPath(rs.getString("musicPhotoPath"));
        music.setFilePath(rs.getString("filePath"));
        music.setPostDate(rs.getDate("postDate"));
        music.setBelongedAlbumId(rs.getInt("belongedAlbumid"));
        music.setBelongedCombinationId(rs.getInt("belongedCombinationid"));
        music.setBelongedMusicianId(rs.getInt("belongedMusicianid"));
        music.setBelongedMusiclistId(rs.getInt("belongedMusiclistid"));
        music.setPosterId(rs.getInt("belongedPosterid"));
        music.setAlbum((Album) albumDao.find(rs.getInt("belongedAlbumid")));
        music.setPoster((User) userDao.find(rs.getInt("belongedPosterid")));
        music.setMusicList((MusicList) musicListDao.find(rs.getInt("belongedMusiclistid")));
//        music.setComments();
//        music.setSingers();
        /**
         * 这两个参数的set要在相应的类Dao层中编写相关的代码
         */

        return music;
    }
}
